package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.DriverOilMainCardBiz;
import com.egao.orderB.entity.DriverOilMainCard;
import com.egao.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 * 数据层实现类
 *
 * @author : Doman
 * @version ：1.0
 * @date : 2021-07-07 17:00:42
 */
@Repository
public class DriverOilMainCardBizImpl extends BaseDao implements DriverOilMainCardBiz {

  @Override
  public JsonResult list(PageResult2<T> page, DriverOilMainCard driverOilMainCard, User loginUser) throws Exception {
    Map<String, String> map = new HashMap<String, String>();
    RowMapper<DriverOilMainCard> rm = new BeanPropertyRowMapper<DriverOilMainCard>(DriverOilMainCard.class);
    StringBuffer sql = new StringBuffer("select d.id,d.organization_id,d.company_name");
    sql.append(",d.card_no,d.price,d.oil_price,d.oil");
    sql.append(",d.remark,d.create_time,d.create_user,d.update_time");
    sql.append(",d.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.username createName ");
    sql.append(" from chf_driver_oil_main_card d ");
    sql.append(" inner join sys_user suser on suser.user_id=d.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=d.organization_id and sogzn.deleted=0 ");
    sql.append(" where d.deleted=0 ");
    if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
      sql.append(" and d.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if (Utils.isNotEmpty(driverOilMainCard)) {
      if (Utils.isNotEmpty(driverOilMainCard.getCardNo())) {
        sql.append(" and instr(d.card_no,:card_no)>0 ");
        map.put("card_no", driverOilMainCard.getCardNo());
      }
      if (Utils.isNotEmpty(driverOilMainCard.getSort()) && Utils.isNotEmpty(driverOilMainCard.getOrder())) {
        sql.append(" order by d." + driverOilMainCard.getSort() + " " + driverOilMainCard.getOrder());
      } else {
        sql.append(" order by d.create_time desc");
      }
    }
    return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
  }

  @Override
  public List<DriverOilMainCard> list(DriverOilMainCard driverOilMainCard, User loginUser) {
    Map<String, String> map = new HashMap<String, String>();
    RowMapper<DriverOilMainCard> rm = new BeanPropertyRowMapper<DriverOilMainCard>(DriverOilMainCard.class);
    StringBuffer sql = new StringBuffer("select d.id,d.organization_id,d.company_name");
    sql.append(",d.card_no,d.price,d.oil_price,d.oil");
    sql.append(",d.remark,d.create_time,d.create_user,d.update_time");
    sql.append(",d.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.username createName ");
    sql.append(" from chf_driver_oil_main_card d ");
    sql.append(" inner join sys_user suser on suser.user_id=d.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=d.organization_id and sogzn.deleted=0 ");
    sql.append(" where d.deleted=0 ");
    if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
      sql.append(" and d.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if (Utils.isNotEmpty(driverOilMainCard)) {

      if(Utils.isNotEmpty(driverOilMainCard.getCardNo())){
        sql.append(" and instr(d.card_no,:card_no)>0 ");
        map.put("card_no", driverOilMainCard.getCardNo());
      }
      if (Utils.isNotEmpty(driverOilMainCard.getSort()) && Utils.isNotEmpty(driverOilMainCard.getOrder())) {
        sql.append(" order by d." + driverOilMainCard.getSort() + " " + driverOilMainCard.getOrder());
      } else {
        sql.append(" order by d.create_time desc");
      }
    }
    getMapParameter(sql, map);
    return jdbcTemplate.query(sql.toString(), map, rm);
  }

  @Override
  public DriverOilMainCard getById(DriverOilMainCard driverOilMainCard) {
    RowMapper<DriverOilMainCard> rm = new BeanPropertyRowMapper<DriverOilMainCard>(DriverOilMainCard.class);
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("select d.id,d.organization_id,d.company_name");
    sql.append(",d.card_no,d.price,d.oil_price,d.oil");
    sql.append(",d.remark,d.create_time,d.create_user,d.update_time");
    sql.append(",d.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.username createName ");
    sql.append(" from chf_driver_oil_main_card d ");
    sql.append(" inner join sys_user suser on suser.user_id=d.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=d.organization_id and sogzn.deleted=0 ");
    sql.append(" where d.deleted=0 and d.id=:id ");
    map.put("id", driverOilMainCard.getId());
    getMapParameter(sql, map);
    return jdbcTemplate.queryForObject(sql.toString(), map, rm);
  }

  @Override
  public boolean save(DriverOilMainCard driverOilMainCard, User loginUser) {
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("insert into chf_driver_oil_main_card (");
    sql.append("organization_id,company_name,card_no,price");
    sql.append(",oil_price,oil,remark,create_user,create_time) values ( ");
    sql.append(":organization_id,:company_name,:card_no,:price");
    sql.append(",:oil_price,:oil,:remark,:create_user,now())");
    map.put("organization_id", loginUser.getOrganizationId() + "");
    map.put("company_name", driverOilMainCard.getCompanyName());
    map.put("card_no", driverOilMainCard.getCardNo());
    map.put("price", "0");
    map.put("oil_price", "0");
    map.put("oil", "0");
    map.put("remark", driverOilMainCard.getRemark());
    map.put("create_user", loginUser.getUserId() + "");
    getMapParameter(sql, map);
    int count = jdbcTemplate.update(sql.toString(), map);
    return count > 0;
  }

  @Override
  public boolean update(DriverOilMainCard driverOilMainCard, User loginUser) {
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("update chf_driver_oil_main_card set ");
    sql.append("company_name=:company_name,card_no=:card_no ");
    sql.append(",remark=:remark ");
    sql.append(",update_time=now(),update_user=:update_user where id=:id ");
    map.put("company_name", driverOilMainCard.getCompanyName());
    map.put("card_no", driverOilMainCard.getCardNo());
    map.put("remark", driverOilMainCard.getRemark());
    map.put("update_user", loginUser.getUserId() + "");
    map.put("id", driverOilMainCard.getId());
    getMapParameter(sql, map);
    int count = jdbcTemplate.update(sql.toString(), map);
    return count > 0;
  }

  @Override
  public boolean recharge(DriverOilMainCard driverOilMainCard, User loginUser) {
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("update chf_driver_oil_main_card set ");
    sql.append("oil_price=:oil_price ");
    if (Utils.isNotEmpty(driverOilMainCard)) {
      if (Utils.isNotEmpty(driverOilMainCard.getPrice())) {
        sql.append(" ,price=price+:price ");
        map.put("price", driverOilMainCard.getPrice());
      }
      if (Utils.isNotEmpty(driverOilMainCard.getOil())) {
        sql.append(" ,oil=oil+:oil ");
        map.put("oil", driverOilMainCard.getOil());
      }
    }
    sql.append(",update_time=now(),update_user=:update_user where id=:id ");
    map.put("oil_price", driverOilMainCard.getOilPrice());
    map.put("remark", driverOilMainCard.getRemark());
    map.put("update_user", loginUser.getUserId() + "");
    map.put("id", driverOilMainCard.getId());
    getMapParameter(sql, map);
    int count = jdbcTemplate.update(sql.toString(), map);
    return count > 0;
  }

  @Override
  public boolean distribution(DriverOilMainCard driverOilMainCard, User loginUser) {
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("update chf_driver_oil_main_card set ");
    sql.append(" update_time=now()  ");
    if (Utils.isNotEmpty(driverOilMainCard)) {
      if (Utils.isNotEmpty(driverOilMainCard.getPrice())) {
        sql.append(" ,price=price-:price ");
        map.put("price", driverOilMainCard.getPrice());
      }
      if (Utils.isNotEmpty(driverOilMainCard.getOil())) {
        sql.append(" ,oil=oil-:oil ");
        map.put("oil", driverOilMainCard.getOil());
      }
    }
    sql.append(",update_user=:update_user where id=:id ");
    map.put("update_user", loginUser.getUserId() + "");
    map.put("id", driverOilMainCard.getId());
    getMapParameter(sql, map);
    int count = jdbcTemplate.update(sql.toString(), map);
    return count > 0;
  }

  @Override
  public boolean remove(DriverOilMainCard driverOilMainCard, User loginUser) {
    int count = 0;
    Map<String, String> map = null;
    if (driverOilMainCard.getId().length() > 0) {
      for (String ID : driverOilMainCard.getId().split(",")) {
        map = new HashMap<String, String>();
        if (ID == null || ID.equals("")) {
          break;
        }
        StringBuffer sql = new StringBuffer("update chf_driver_oil_main_card set deleted=1" +
                ",update_user=:update_user, update_time=now() "
                + " where id =:id ");
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", ID);
        getMapParameter(sql, map);
        count = jdbcTemplate.update(sql.toString(), map);
      }
    }
    return count > 0;
  }
}
